package articles_api

import (
	"gin_study_blog/common"
	"gin_study_blog/common/http"
	"gin_study_blog/models"
	"gin_study_blog/utils"
	"github.com/gin-gonic/gin"
	"github.com/liu-cn/json-filter/filter"
)

type ArticleSearchRequest struct {
	models.PageInfo
	Tags string `json:"tags" form:"tags"`
}

func (ArticlesApi) ArticleViewList(c *gin.Context) {
	var cr ArticleSearchRequest
	if err := c.ShouldBindQuery(&cr); err != nil {
		http.FailWithCode(http.ParamsError, c)
		return
	}
	list, count, err := utils.CommList(utils.Option{
		PageInfo: cr.PageInfo,
		Fields:   []string{"title", "content"},
		Tags:     cr.Tags,
	})
	if err != nil {
		common.Log.Error(err.Error())
		http.FailWithMessage("查询失败", c)
		return
	}

	data := filter.Omit("list", list)
	if len(list) <= 0 {
		data = make([]ArticleModel, 0)
	}
	http.IsOkWithList(data, int64(count), c)
}
